/*
 * @文件描述: 
 * @版本: 
 * @作者: 周晨阳
 * @Date: 2021-04-18 15:10:14
 */
#include "MultiMeterPre.h"
#include "CurrentMeter.h"
#include "GUI.h"
#include "ResistanceMeter.h"
#include "VoltageMeter.h"
#include "multiMeterUICodes.h"

/**************变量声明**************/
//=1的时候代表屏幕被冻结
unsigned char holdTriggerSign = 0;
unsigned char mode;

static unsigned char last_Unit;
static float last_value;

/**************函数定义**************/

/**
 * @description: GUI初始化
 * @param {type}
 * @return {type}
 */
void MultiMeter_GUIInit()
{

    holdTriggerSign = 0;
    MultiMeterUI_Init();
    //   ILI9341_Init();
    //   XPT2046_Init();
    //   Calibrate_or_Get_TouchParaWithFlash(3, 0);
    //   ILI9341_DispStringLine_EN(LINE(0), "TFT init done......");
    //   ILI9341_DispStringLine_EN(LINE(1), "loading system ......");
    //   GUI_Init(); // ucgui初始化
    //  printf("GUI init done...\n");
}

/**
 * @description: 设置ui显示的量程
 * @param {char} *range
 * @return {*}
 */
void setDisRange(unsigned char mode)
{
    int Range;
    static char dispBuff[15];
    switch (mode)
    {
    case MODE_VOLTAGE:
        if (getVoltageFactor() == 1)
        {
            Range = 160;
        }
        else if (getVoltageFactor() == 10)
        {
            Range = 16;
        }
        sprintf(dispBuff, "-%d ~ +%d V", Range, Range);
        UIsetRange(dispBuff);
        break;
    case MODE_CURRENT:
        if (getCurrentFactor() == 1)
        {
            Range = 300;
            sprintf(dispBuff, "-%d ~ +%d mA", Range, Range);
        }
        else if (getCurrentFactor() == 5)
        {
            Range = 30;
            sprintf(dispBuff, "-%d ~ +%d mA", Range, Range);
        }

        UIsetRange(dispBuff);
        break;
    case MODE_RESISTANCE:

        if (getResistanceFactor() == 1)
        {
            Range = RES_X1_RANGE;
            sprintf(dispBuff, "%d ~ %d MR", 0, Range);
        }
        else if (getResistanceFactor() == 10)
        {
            Range = RES_X10_RANGE;
            sprintf(dispBuff, "%d ~ %d KR", 0, Range);
        }

        UIsetRange(dispBuff);
        break;
    }
}
/**
 * @description:
 * 设置屏幕显示的模式,仅仅会改变屏幕的显示，而不会真正改变电表模式，由系统内部调用
 * @param {type}
 * @return {type}
 */
void setDisplayMode(unsigned char mode)
{
    mode = mode;
    setDisRange(mode);

    switch (mode)
    {
    case MODE_VOLTAGE:

        UIsetMode(MODE_VOLTAGE);
        setUnit(UNIT_V);
        break;
    case MODE_CURRENT:

        UIsetMode(MODE_CURRENT);
        setUnit(UNIT_A);
        break;
    case MODE_RESISTANCE:

        UIsetMode(MODE_RESISTANCE);
        setUnit(UNIT_OHM);
        break;
    default:
        break;
    }
}

/**
 * @description: //获取当前显示模式
 * @param {type}
 * @return {type}
 */
unsigned char getDisplayMode() { return mode; }

/**
 * @description: hold功能，由按键模块和UI调用
 * @param {type}
 * @return {type}
 */
void multiMeterHoldTrigger()
{

    UIsetRunningState(holdTriggerSign);
    holdTriggerSign = ~holdTriggerSign;
}
/**
 * @description: 设置显示的单位（私有函数）
 * @param {type}
 * @return {type}
 */
static void drawUnitByCode(unsigned char code)
{
    switch (code)
    {

    case UNIT_V:
        UIsetDisUnit("V");
        // ILI9341_DispStringLine_EN(LINE(2), "V");
        break;
    case UNIT_MV:
        UIsetDisUnit("mV");
        // ILI9341_DispStringLine_EN(LINE(2), "mV");
        break;
    case UNIT_UV:
        UIsetDisUnit("uV");
        // ILI9341_DispStringLine_EN(LINE(2), "uV");
        break;
    case UNIT_A:
        UIsetDisUnit("A");
        // ILI9341_DispStringLine_EN(LINE(2), "A");
        break;
    case UNIT_MA:
        UIsetDisUnit("mA");
        // ILI9341_DispStringLine_EN(LINE(2), "mA");
        break;
    case UNIT_UA:
        UIsetDisUnit("uA");
        // ILI9341_DispStringLine_EN(LINE(2), "uA");
        break;
    case UNIT_OHM:
        UIsetDisUnit("R");
        // ILI9341_DispStringLine_EN(LINE(2), "ohm");
        break;
    case UNIT_KOHM:
        UIsetDisUnit("KR");
        // ILI9341_DispStringLine_EN(LINE(2), "Kohm");
        break;
    case UNIT_MOHM:
        UIsetDisUnit("MR");
        // ILI9341_DispStringLine_EN(LINE(2), "Mohm");
        break;
    }
}
/**
 * @description: 设置当前主界面所显示的单位，由数值更新模块调用
 * @param {unsignedchar} unit
 * @return {*}
 */
void setUnit(unsigned char unit)
{
    if (!holdTriggerSign)
    {

        drawUnitByCode(unit);
        last_Unit = unit;
    }
    else
    {

        drawUnitByCode(last_Unit);
    }
}
/**
 * @description: 设置主界面显示的数字,由数值更新模块调用
 * @param {float} 要显示的数字
 * @param {uint8_t} 显示数字的位数
 * @return {type}
 */
void setNumTitle(float num, uint8_t digit)
{

    // static char dispBuff[15];
    if (!holdTriggerSign)
    {
        // sprintf(dispBuff, "value:%f", num);
        // ILI9341_DispStringLine_EN(LINE(1), dispBuff);
        UIsetNum(num, digit);
        last_value = num;
    }
    else
    {
        // sprintf(dispBuff, "value:%f", last_value);
        UIsetNum(last_value, digit);
        // ILI9341_DispStringLine_EN(LINE(1), dispBuff);
    }
}
/******************************************************/

/**
 * @description: 切换电表的模式，由ui调用
 * @param {int} mode
 * @return {*}
 */
void setMeterMode(int mode) { switchMode(mode); }
/**
 * @description: 获取内部的电表模式，非显示模式，由ui调用
 * @param {*}
 * @return {*}
 */
int getMeterMode() { return getCurrentMode(); }

/**
 * @description:
 * 设置不同表的量程，每一个表都有两个量程，该函数由ui调用，由用户决定当前的量程
 * @param {int} mode
 * @param {int} range
 * @return {*}
 */
void setRange(int mode)
{
    switch (mode)
    {
    case MODE_VOLTAGE:
        if (getVoltageFactor() == 1)
        {
            changeVoltageFactor(10);
        }
        else if (getVoltageFactor() == 10)
        {
            changeVoltageFactor(1);
        }

        break;

    case MODE_CURRENT:
        if (getCurrentFactor() == 1)
        {
            changeCurrentFactor(5);
        }
        else if (getCurrentFactor() == 5)
        {
            changeCurrentFactor(1);
        }

        break;

    case MODE_RESISTANCE:
        if (getResistanceFactor() == 1)
        {
            changeResistanceFactor(10);
        }
        else if (getResistanceFactor() == 10)
        {
            changeResistanceFactor(1);
        }
        break;
    }
    setDisRange(mode);
}

/**
 * @brief : 
 * @param {float} angle
 * @return {*}
 */
void V_setNiddleAngle(float angle)
{
    UISetAngle(angle);
}
void V_drawNiddle()
{
    onViewDrawNiddel();
}